package fore;

import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

import back.CLOCK;
import back.MMU;
import javax.swing.JScrollPane;

public class UI_TLB extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Object obj=new Object();
	public static final int WIDTH = Toolkit.getDefaultToolkit()
			.getScreenSize().width;
	public static final int HEIGHT = Toolkit.getDefaultToolkit()
			.getScreenSize().height;
	public static JTable table;

	/**
	 * Launch the application.
	 */
	String vname[]=new String[] {"\u9875\u53F7", "\u5185\u5B58\u5757\u53F7"};

	/**
	 * Create the frame.
	 */
	public UI_TLB() {
		setTitle("\u5FEB\u8868");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setSize(600,500);
		setLocation((WIDTH-600)/2, (HEIGHT-500)/2);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(22, 74, 539, 188);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
			},
			vname
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			boolean[] columnEditables = new boolean[] {
				false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
	}
	public void start()
	{
		ReFresh t=new ReFresh();
		t.start();
	}
	class ReFresh extends Thread
	{
		public void run()
		{
			int k=0;
			while(true)
			{
				synchronized(obj)
				{
					if(UI_ALL.pause_refresh==0)
						k=0;
					else
					{
						k++;
					}
				}
				if(k!=1)
				{
						double time1,time2;
						time1=time2=CLOCK.time;
						while(true)
						{
							synchronized(obj)
							{
								if((time2-time1)>1)
									break;
								else
									time2=CLOCK.time;
							}
						}
				}
				
				Vector<Vector<String>> vData = new Vector<Vector<String>>();
				synchronized(obj) 
				{
					
							for(int i=0;i<MMU.tlb.length;i++)
							{
								Vector<String> vRow = new Vector<String>();
								vRow.add(""+MMU.tlb.get_logicnumber(i));
								vRow.add(""+MMU.tlb.get_pagenum(i));
								vData.add(vRow);
							}
					
					if(vData.isEmpty()==false)
					{
						Vector<String> vName=new Vector<String>();
						for(int i=0;i<vname.length;i++)
							vName.add(vname[i]);
						DefaultTableModel model = new DefaultTableModel(vData, vName);
						table.setModel(model);
					}
					else
					{
						table.setModel(new DefaultTableModel());
					}
				}
				
			}
		}
	}
}
